from tppt.data.models import *

ev2capt={'ok':'ok', 'discovery':'new', 'wrong': 'bad', 'fail': 'fail', 'nothing': 'none'}
def evaluate_result(result):
	problem_status=result.problem.status
	result_type=result.result_type
	
	try:
		evaluation=ResultEvaluation.objects.get(problem_status=problem_status, result_type=result_type)
		res={'ok':evaluation.ok, 'wrong':evaluation.wrong, 'discovery':evaluation.discovery, 'fail': evaluation.fail}
	except ResultEvaluation.DoesNotExist:
		res={'ok':False, 'wrong':False, 'discovery':False, 'fail': False}
	if res['discovery']:
		res['type']='discovery'
	elif res['wrong']:
		res['type']='wrong'
	elif res['fail']:
		res['type']='fail'
	elif res['ok']:
		res['type']='ok'
	else:
		res['type']='nothing'
	res['caption']=ev2capt[res['type']]
	return res
	
def get_results_query(problem, prover):
	query=Result.objects.filter(problem=problem, prover=prover).order_by('-added')
	return query

def get_problem_prover_table(problems, provers):
	#we use dictionaries instead of plain lists so it would be easier to access the values
	#from templates
	rows={}
	for problem in problems:
		cols={}
		problem_result_count=0
		for prover in provers:
			results_data=[]
			results=get_results_query(problem, prover)
			for result in results:
				res_ev=evaluate_result(result)
				result_data={
							'type': res_ev['type'], 
							'time': result.time,
							'caption': res_ev['caption'] 
							}
				results_data.append(result_data)
			cols[prover.pk]=results_data
			problem_result_count+=len(results_data)
			
		cols['result_count']=problem_result_count
		rows[problem.pk]=cols
	return rows;

